﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace StatusPlan3
{
    /// <summary>
    /// Interaction logic for Window_SelectUsers.xaml
    /// </summary>
    public partial class Window_SelectUsers : Window
    {
        ProjectGroup projectGroup;
        Users usersORG;
        String filter = "";

        public Window_SelectUsers()
        {
            InitializeComponent();
        }

        public void setData(ProjectGroup p)
        {
            usersORG = new Users();
            projectGroup = p;
            Users users = StatusPlanData.statusPlanHandler.getAllUsers();
            foreach (var user in users)
            {
                Boolean add = true;
                foreach (var user2 in projectGroup.projectMembers)
                {
                    if (user2.id == user.id)
                    {
                        add = false;
                        break;
                    }
                }

                if (add)
                    usersORG.Add(user);
            }

            populateTreeView();

            this.ShowDialog();
            filter_TextBox.Focus();
        }

        private void populateTreeView()
        {
            radTreeView1.Items.Clear();
            foreach (User u in usersORG)
            {
                if (u.fullName.Trim().ToLower().Contains(filter.Trim().ToLower()) || filter.Trim().Length == 0)
                {
                    radTreeView1.Items.Add(u);
                }
            }
            radTreeView1.DisplayMemberPath = "fullName";
        }

        private void OkButto_Click(object sender, RoutedEventArgs e)
        {
            Users usersToAdd = new Users();
            foreach (User u in radTreeView1.CheckedItems)
            {
                usersToAdd.Add(u);
            }

            if (usersToAdd.Count == 0)
            {
                MessageBox.Show("Ongin brúkari valdur", "Feilur", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else
            {
                StatusPlanData.statusPlanHandler.addProjectGroupMembers(usersToAdd,projectGroup);
                this.Close();
            }
        }

        private void CancelButto_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void filter_TextBox_KeyUp(object sender, KeyEventArgs e)
        {
            filter = filter_TextBox.Text;
            populateTreeView();
        }
    }
}
